package com.leo.model;

/**
 * @ClassName CycleArrayQuene
 * @Description 循环队列
 * @Author wangss
 * @date 2022.02.08 09:00
 * @Version 1.0
 */
public class CycleArrayQuene {

    private String[] item;

    private int n;

    private int head;
    private int tail;

    public CycleArrayQuene(int capacity) {
        item = new String[capacity];
        this.n = capacity;
        this.head = 0;
        this.tail = 0;
    }

    public boolean push(String itm) {
        // 存入队列时判断是否已经满了
        if ((tail + 1) % n == head) {
            return false;
        }

        item[tail] = itm;
        tail = (tail + 1) % n;
        return true;
    }

    public String pop() {
        // 取出数据时判断队列是否为空
        if (tail == head) {
            return null;
        }

        String ret = item[head];
        head = (head + 1) % n;
        return ret;
    }
}
